package main;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Number;
import java.io.File;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		WritableWorkbook wworkbook;
		String base = "http://www.obviously-marvelous.com";
		String blog="Obviously Marvelous";
	      wworkbook = Workbook.createWorkbook(new File(blog+".xls"));
	      WritableSheet wsheet = wworkbook.createSheet("First Sheet", 0);
	      Label label = new Label(0, 0, "Author");
	      wsheet.addCell(label);
	      label = new Label(1, 0, "Blog Name");
	      wsheet.addCell(label);
	      label = new Label(2, 0, "Post Title");
	      wsheet.addCell(label);
	      label = new Label(3, 0, "Post Body");
	      wsheet.addCell(label);
	      label = new Label(4, 0, "Topic");
	      wsheet.addCell(label);
	      label = new Label(5, 0, "URL");
	      wsheet.addCell(label);
	 
		int count=0;
		ArrayList<String> urls = new ArrayList<String>();
		
		for(int i=1;i<=20;i++)
		{		
			Document doc = null;
			try{
			doc = Jsoup.connect(base+"/page/"+i+"/").get();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				continue;
			}
			//System.out.println(doc);
			Elements x =doc.getElementsByTag("a");
			//System.out.println(x);
			//Iterator<Element> it = x.iterator();
			
			for(Element link:x)
			{
				String postURL = link.attr("href");
				if(!urls.contains(postURL) && postURL.startsWith(base) && !postURL.equals(base+"/") && !postURL.equals(base) && !postURL.equals(base+"/about/") 
						&& !postURL.equals(base+"/contact/") && !postURL.startsWith(base+"/blog/page") && !postURL.startsWith(base+"/page")
						&& !postURL.startsWith(base+"/category") && !postURL.contains("share=") && !postURL.contains("#comments") && !postURL.contains("#respond") && !postURL.startsWith(base+"/author")
						 && !postURL.startsWith(base+"/tag")  && !postURL.startsWith(base+"/wp"))
				{	
					count++;
					urls.add(postURL);
					System.out.println(count+" "+postURL);
					Document post;
					try{
					post = Jsoup.connect(postURL).get();
					}
					catch(Exception e)
					{
						count--;
						continue;
					}
					String title = post.getElementsByTag("title").html();
					String body = post.getElementsByAttributeValue("class", "entry-content").text();

					//String body = post.getElementsByAttributeValue("class", "entry").text();
				//	String body = post.getElementsByAttributeValue("class", "post-info").text();

					//				String author = post.getElementsByAttributeValue("rel", "author").text();
//					System.out.println(author);
//					if(author.equals(""))
//					{
//						count--;
//						continue;
//					}
					label = new Label(0,count,blog);
					wsheet.addCell(label);
					label = new Label(1,count,blog);
					wsheet.addCell(label);
					label = new Label(2,count,title);
					wsheet.addCell(label);
					label = new Label(3,count,body);
					wsheet.addCell(label);
					label = new Label(4,count,"motherhood");
					wsheet.addCell(label);
					label = new Label(5,count,postURL);
					wsheet.addCell(label);
				}
			}
		}
	      wworkbook.write();
	      wworkbook.close();

//		for(int i=1;i<=100;i++)
//		{
//			URL oracle = new URL("http://not-your-average-mom.com/page/"+i);
//			BufferedReader in = new BufferedReader(
//					new InputStreamReader(oracle.openStream()));
//
//			
//			String inputLine;
//			while ((inputLine = in.readLine()) != null)
//				System.out.println(inputLine);
//			in.close();
//		}
	}

}